﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.BLL.NetBar;
using Jzy.Core.Common.Web;
using Jzy.Core.Common;
using Jzy.Core.Utility;
using FortuneHeaven.Entity.NetBar;
using System.Web.Security;

namespace FortuneHeaven.Admin.Pages.NetBar
{
    public partial class Edit : PageEdit
    {
        private NetBarBO NetBar
        {
            get
            {
                return NetBarBO.Instance<NetBarBO>();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDrp();

                if (!string.IsNullOrEmpty(this.PKID))
                {
                    DispInfo();
                }                
            }
            AddAttr();
        }

        private void AddAttr()
        {
            //WebUtil.SetTimeBox(EffectiveDate, false);
            //WebUtil.SetTimeBox(ExpirationDate, false);
        }

        private void DispInfo()
        {
            NetBarInfo info = NetBar.GetInfo(this.PKID);
            EntityUtil.SetControl(pnlMain, info);


            drpStatus.SelectedValue = info.Status.ToString();

            if (info.EffectiveDate != null)
            {
                this.EffectiveDate.Text = info.EffectiveDate.Value.ToString("yyyy-MM-dd");
            }
            if (info.ExpirationDate != null)
            {
                this.ExpirationDate.Text = info.ExpirationDate.Value.ToString("yyyy-MM-dd");
            }

        }

        private void BindDrp()
        {
            List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(NetBarStatus));

            drpStatus.DataSource = list;
            drpStatus.DataTextField = "Name";
            drpStatus.DataValueField = "ID";
            drpStatus.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            
            NetBarInfo info = this.NetBar.GetInfo(this.PKID);
            EntityUtil.SetEntity(pnlMain, info);
            info.Status = byte.Parse(drpStatus.SelectedValue);
            if (string.IsNullOrEmpty(this.PKID))
            {
                info.RigisterDate = DateTime.Now;
                info.License = FormsAuthentication.HashPasswordForStoringInConfigFile(Guid.NewGuid().ToString(), "MD5").Substring(0,16).ToLower();
            }      
       
            this.NetBar.Save(info);
            JSUtil.RefreshView();
            
        }
    }
}